<?php
/**
 * Pertyler
 * Table Page
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require 'main.php';

if(isset($page->Table))
{
	$table = Pertyler_Table::Select($page->Table);

	if(isset($page->Row))
	{
		$table = $table->SelectParentTable();

		$row = $table->SelectRow($page->Row);

		if(!$row)
			throw new Exception('Row cannot found');

		$tree = $page->Tree($row);

		$tree->AddParameter('ParentTable', $table->id());

		$tree->AddLink('ObjectBrowser.php', 'leftMenu');

		$table = $row->SelectTable($page->Table);

		$tree->AddParameter('Table', $table->id());

		$branchlet = $tree->Add(new TreeBranch($table));
	}
	else
	{
		$tree = $page->Tree($table);

		$branchlet = $tree;
	}

	$branchlet->AddParameter('ParentTable', $page->Table);

	if($branchlet->Pressed() == 'add')
	{
		$branchlet->AddButton('add_okay');

		$branchlet->AddButton('cancel');

		$branch = $branchlet->Add(new TreeBranch('New Row'));

		if($table->SelectPrivilege('approve'))
		{
			$leaf = $branch->Add(new TreeLeaf('Last Changed Time', new Date));

			if($table->SelectPrivilege('approve'))
				$leaf->Colour('#006666');

			$leaf = $branch->Add(new TreeLeaf('Last Changed Moderator', Pertyler_Role_Moderator::Select()));
		}

		foreach($table->SelectColumnArray() AS $column)
			switch($column->ColumnType())
			{
				case 'dropdown':
					$html = '
	<select name="column' . $column->Priority() . '" >
		<option value="" >
		</option>';

					if(($column->Priority() != 1) OR (!$table->ConnectorTable()))
						$rowArray = $column->RelatedTable()->SelectRowArray();
					else
						$rowArray = $column->RelatedTable()->SelectNonexistentRowArray($table);

					foreach($rowArray AS $row)
						$html .= '
		<option value="' . $row->id() . '" >
			' . $row . '
		</option>';

					$html .= '
	</select>';
		
					$branch->Add(new TreeLeaf($column, $html));

					break;
				case 'integer':
					$leaf = $branch->Add(new TreeLeaf($column, '<input type="text" name="column' . $column->Priority() . '" maxlength="6" />'));

					break;
				case 'string':
					$leaf = $branch->Add(new TreeLeaf($column, '<input type="text" name="column' . $column->Priority() . '" maxlength="60" />'));

					break;
				case 'text':
					$html = <<<EOF
<input type="button" value="Bold" onclick="InsertHTMLTag('textArea{$column->Priority()}', 'b');" >
<input type="button" value="Italic" onclick="InsertHTMLTag('textArea{$column->Priority()}', 'i');" >
<input type="button" value="Paragraph" onclick="InsertHTMLTag('textArea{$column->Priority()}', 'p');" >
<input type="button" value="&lt;" onclick="InsertCharacter('textArea{$column->Priority()}', '&amp;lt;');" >
<input type="button" value="&gt;" onclick="InsertCharacter('textArea{$column->Priority()}', '&amp;gt;');" >
<input type="button" value="&amp;" onclick="InsertCharacter('textArea{$column->Priority()}', '&amp;amp;');" >
<input type="button" value="&quot;" onclick="InsertTwoCharacter('textArea{$column->Priority()}', '&amp;quot;');" >
EOF;

					$branch->Add(new TreeLeaf($html));

					$branch->Add(new TreeLeaf($column, '<textarea rows="20" cols="50%" id="textArea' . $column->Priority() . '" name="column' . $column->Priority() . '" ></textarea>'));

					break;
				case 'paragraph':
					$html = <<<EOF
<input type="button" value="Bold" onclick="InsertHTMLTag('textArea{$column->Priority()}', 'b');" >
<input type="button" value="Italic" onclick="InsertHTMLTag('textArea{$column->Priority()}', 'i');" >
<input type="button" value="&lt;" onclick="InsertCharacter('textArea{$column->Priority()}', '&amp;lt;');" >
<input type="button" value="&gt;" onclick="InsertCharacter('textArea{$column->Priority()}', '&amp;gt;');" >
<input type="button" value="&amp;" onclick="InsertCharacter('textArea{$column->Priority()}', '&amp;amp;');" >
<input type="button" value="&quot;" onclick="InsertTwoCharacter('textArea{$column->Priority()}', '&amp;quot;');" >
EOF;

					$branch->Add(new TreeLeaf($html));

					$branch->Add(new TreeLeaf($column, '<textarea rows="14" cols="50%" id="textArea' . $column->Priority() . '" name="column' . $column->Priority() . '" ></textarea>'));

					break;
				case 'date':
					$branch->Add(new TreeLeaf($column, '<input type="text" name="column' . $column->Priority() . '" maxlength="11" />'));

					break;
				case 'boolean':
					if(($column->Priority() == 2) AND ($column->backgroundName() == 'Unpublished'))
						$branch->Add(new TreeLeaf($column, false));
					else if(($column->Priority() == 2) AND ($column->backgroundName() == 'Alternative'))
						$branch->Add(new TreeLeaf($column, true));
					else
					{
						$html = '<input type="checkbox" name="column' . $column->Priority() . '" />';

						$branch->Add(new TreeLeaf($column, $html));
					}

					break;
				case 'float':
					$branch->Add(new TreeLeaf($column, '<input type="text" name="column' . $column->Priority() . '" maxlength="12" />'));

					break;
			}
	}
	else if($branchlet->Pressed() == 'delete')
	{
		$branchlet->AddButton('delete_okay');

		$branchlet->AddButton('cancel');

		$html = '
<select name="row" >';

		foreach($table->SelectRowArray() AS $row)
			$html .= '
	<option value="' . $row->id() . '" >
		' . $row . '
	</option>';

		$html .= '
</select>';

		$leaf = $branchlet->Add(new TreeLeaf($html));
	}
	else
	{
		if((isset($page->Row)) OR (!$table->SelectParentTable()))
		{
			if(($table->SelectPrivilege('select')) AND (Pertyler_Role::Select()->SelectRolePrivilege($table->SelectPrivilege('select'))))
				$branchlet->AddButton('all');

			if(($table->SelectPrivilege('insert')) AND (Pertyler_Role::Select()->SelectRolePrivilege($table->SelectPrivilege('insert'))))
				$branchlet->AddButton('add');

			if(($table->SelectPrivilege('delete')) AND (Pertyler_Role::Select()->SelectRolePrivilege($table->SelectPrivilege('delete'))))
				$branchlet->AddButton('delete');
		}

		if(($table->SelectPrivilege('select')) AND (Pertyler_Role::Select()->SelectRolePrivilege($table->SelectPrivilege('select'))) AND ($table->SelectColumn(2)) AND ($table->SelectColumn(2)->backgroundName() == 'Unpublished'))
			$branchlet->AddButton('unpublished');

		if(($table->SelectPrivilege('approve')) AND (Pertyler_Role::Select()->SelectRolePrivilege($table->SelectPrivilege('approve'))))
			$branchlet->AddButton('unapproved');

		$search = false;

		if($branchlet->Pressed() == 'search_okay')
		{
			foreach($table->SelectColumnArray() AS $column)
				if(($column->ColumnType() == 'string') OR ($column->ColumnType() == 'paragraph') OR ($column->ColumnType() == 'text'))
					if($_POST['column' . $column->Priority()])
						$columnArray[$column->Priority()] = $_POST['column' . $column->Priority()];
					else
						$columnArray[$column->Priority()] = NULL;

			try
			{
				$rowArray = $table->SelectSearchedRowArray($columnArray);

				foreach($rowArray AS $row)
				{
					$leaf = $branchlet->Add(new TreeLeaf($row));

					$leaf->AddParameter('Row', $row->id());

					if(($table->SelectTableArray()) OR $table->ConnectorTable())
						$leaf->AddLink('ObjectBrowser.php', 'leftMenu');
					else
						$leaf->AddLink('row.php');
				}
			}
			catch(Exception $e)
			{
				$branchlet->AddProperty('Search failed');
			}

			if(isset($_POST['column1']))
			{
				function SearchChildTables(TreeBranchlet $branchlet, Pertyler_Table $table, $searchedNameColumnValue)
				{
					foreach($table->SelectTableArray() AS $table)
						if(($table->SelectPrivilege('select')) AND (Pertyler_Role::Select()->SelectRolePrivilege($table->SelectPrivilege('select'))))
						{
							if(($table->SelectColumn(1)->ColumnType() == 'string') OR ($table->SelectColumn(1)->ColumnType() == 'paragraph') OR ($table->SelectColumn(1)->ColumnType() == 'text'))
								try
								{
									
									$rowArray = $table->SelectSearchedRowArray(array(1 => $searchedNameColumnValue));

									if($rowArray)
									{
										$branchlet = $branchlet->Add(new TreeBranchlet($table));

										$branchlet->AddParameter('ParentTable', $table->id());

										foreach($rowArray AS $row)
										{
											$leaf = $branchlet->Add(new TreeLeaf($row));

											$leaf->AddParameter('Row', $row->id());

											if(($table->SelectTableArray()) OR $table->ConnectorTable())
												$leaf->AddLink('ObjectBrowser.php', 'leftMenu');
											else
												$leaf->AddLink('row.php');
										}
									}
								}
								catch(Exception $e)
								{
									$branchlet = $branchlet->Add(new TreeBranchlet($table));

									$branchlet->AddProperty('Search failed');
								}

							SearchChildTables($branchlet, $table, $searchedNameColumnValue);
						}
				}

				SearchChildTables($branchlet, $table, $_POST['column1']);
			}
		}
		else if($branchlet->Pressed() == 'unapproved')
		{
			foreach($table->SelectUnapprovedRowArray() AS $row)
			{
				$leaf = $branchlet->Add(new TreeLeaf($row));

				$leaf->AddParameter('Row', $row->id());

				if(($table->SelectTableArray()) OR $table->ConnectorTable())
					$leaf->AddLink('ObjectBrowser.php', 'leftMenu');
				else
					$leaf->AddLink('row.php');
			}
		}
		else if(($branchlet->Pressed() == 'all') or ((!$branchlet->Pressed()) and (isset($page->Row))))
		{
			foreach($table->SelectRowArray() AS $row)
			{
				$leaf = $branchlet->Add(new TreeLeaf($row));

				$leaf->AddParameter('Row', $row->id());

				if(($table->SelectTableArray()) OR $table->ConnectorTable())
					$leaf->AddLink('ObjectBrowser.php', 'leftMenu');
				else
					$leaf->AddLink('row.php');
			}
		}
		else if($branchlet->Pressed() == 'unpublished')
		{
			$branch = $branchlet->Add(new TreeBranch($table->SelectColumn(2)));

			$branch->AddParameter('ParentTable', $table->id());

			foreach($table->SelectUnpublishedRowArray() AS $row)
			{
				$leaf = $branch->Add(new TreeLeaf($row));

				$leaf->AddParameter('Row', $row->id());

				$leaf->AddLink('row.php');
			}
		}
		else if($branchlet->Pressed() == 'add_okay')
		{
			foreach($table->SelectColumnArray() AS $column)
				switch($column->ColumnType())
				{
					case 'dropdown':
						if($_POST['column' . $column->Priority()])
							$columnArray[$column->Priority()] = $column->RelatedTable()->SelectRow($_POST['column' . $column->Priority()]);
						else
							$columnArray[$column->Priority()] = NULL;

						break;
					case 'integer':
						if($_POST['column' . $column->Priority()])
							$columnArray[$column->Priority()] = (int) $_POST['column' . $column->Priority()];
						else
							$columnArray[$column->Priority()] = NULL;

						break;
					case 'string':
						if($_POST['column' . $column->Priority()])
							$columnArray[$column->Priority()] = $_POST['column' . $column->Priority()];
						else
							$columnArray[$column->Priority()] = NULL;

						break;
					case 'text':
						if($_POST['column' . $column->Priority()])
							$columnArray[$column->Priority()] = $_POST['column' . $column->Priority()];
						else
							$columnArray[$column->Priority()] = NULL;

						break;
					case 'paragraph':
						if($_POST['column' . $column->Priority()])
							$columnArray[$column->Priority()] = $_POST['column' . $column->Priority()];
						else
							$columnArray[$column->Priority()] = NULL;

						break;
					case 'date':
						if($_POST['column' . $column->Priority()])
							$columnArray[$column->Priority()] = Date::Select($_POST['column' . $column->Priority()]);
						else
							$columnArray[$column->Priority()] = NULL;

						break;
					case 'boolean':
						if(($column->Priority() == 2) AND ($column->backgroundName() == 'Unpublished'))
							$columnArray[2] = false;
						else if(($column->Priority() == 2) AND ($column->backgroundName() == 'Alternative'))
							$columnArray[2] = true;
						else
							if(isset($_POST['column' . $column->Priority()]))
								$columnArray[$column->Priority()] = true;
							else
								$columnArray[$column->Priority()] = false;

						break;
					case 'float':
						$columnArray[$column->Priority()] = (float) $_POST['column' . $column->Priority()];

						break;
				}

			try
			{
				$row = $table->InsertRow($columnArray);

				$branchlet->AddProperty('Inserted
<script type="text/javascript">
parent.leftMenu.document.location.reload();
</script>');

				$leaf = $branchlet->Add(new TreeLeaf($row));

				$leaf->AddParameter('Row', $row->id());

				if(($table->SelectTableArray()) OR ($table->ConnectorTable()))
					$leaf->AddLink('ObjectBrowser.php', 'leftMenu');
				else
					$leaf->AddLink('row.php');

				if($table->SelectPrivilege('approve'))
					$leaf->Colour('#006666');
			}
			catch(Exception $e)
			{
				$branchlet->AddProperty('Insert failed');
			}
		}
		else if($branchlet->Pressed() == 'delete_okay')
		{
			$row = $table->SelectRow($_POST['row']);
			
			$branchlet->Add(new TreeLeaf($row));

			try
			{
				$row->Delete();

				$branchlet->AddProperty('Deleted
<script type="text/javascript">
parent.leftMenu.document.location.reload();
</script>');
			}
			catch(Exception $e)
			{
				$branchlet->AddProperty('Cannot deleted');
			}
		}
		else
			$search = true;

		if((!isset($page->Row)) AND ($table->SelectPrivilege('select')) AND (Pertyler_Role::Select()->SelectRolePrivilege($table->SelectPrivilege('select'))))
		{
			if($search)
			{
				$rowCount = 0;

				foreach($table->SelectColumnArray() AS $column)
					if(($column->ColumnType() == 'string') OR ($column->ColumnType() == 'paragraph') OR ($column->ColumnType() == 'text'))
					{
						$leaf = $branchlet->Add(new TreeLeaf($column, '<input type="text" name="column' . $column->Priority() . '" maxlength="60" />'));

						$rowCount++;
					}

				if($rowCount)
					$branchlet->AddButton('search_okay');
			}
			else
				$branchlet->AddButton('search');
		}
	}

	echo $tree;
}